/*
导入数据（Import into script）
Imports (1 entry)
var table: Table users/username/assets
  table可替换为其他变量名
Example：
  var HarbinShp: Table users/username/Harbin2022
*/

//添加图层
function addMyLayer()
{
  var Harbin = HarbinShp;
  Map.centerObject(Harbin,6);
  Map.addLayer(Harbin,{},"Harbin");
}

addMyLayer();

//S2云掩膜
function maskS2clouds(image)
{
  var qa = image.select('QA60');

  var cloudBitMask = 1 << 10;
  var cirrusBitMask = 1 << 11;

  var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
    .and(qa.bitwiseAnd(cirrusBitMask).eq(0));
  
  return image.updateMask(mask);
}

//加载S2影像
function loadS2Data()
{
  //时间段设置
  var startDate = ee.Date('2021-01-01');
  var endDate =  ee.Date('2021-12-31');

  var S2Data = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
    .filterBounds(HarbinShp)
    .filterDate(startDate,endDate)
    .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',10))
    .map(maskS2clouds);
  print(S2Data);

  var rgbParams = {min:0,max:3000,bands:['B4','B3','B2']};
  Map.addLayer(S2Data.median(),rgbParams,'S2 Monthly Average RGB');
}

loadS2Data();

//加载S1影像数据（VH）
function loadS1Data()
{
  //时间段设置
  var startDate = ee.Date('2021-01-01');
  var endDate =  ee.Date('2021-12-31');
  
  //获取S1影像
  var S1Data = ee.ImageCollection('COPERNICUS/S1_GRD')
    .filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'VH'))
    .select('VH')
    .filter(ee.Filter.bounds(HarbinShp))
    .filter(ee.Filter.eq('instrumentMode', 'IW'))
    .filter(ee.Filter.eq('resolution_meters', 10))
    .filter(ee.Filter.date(startDate,endDate));
  
  print('S1',S1Data);
  var VH = S1Data.mean().clipToCollection(HarbinShp);
  Map.addLayer(S1Data,{},"S1Harbin");
}

loadS1Data();